<?php
require_once 'config/database.php';
session_start();

if (!isset($_SESSION['user_id']) || $_SESSION['role'] != 'admin') {
    header('Location: login.php');
    exit;
}

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $model_number = $conn->real_escape_string($_POST['model_number']);
    $production_batch = $conn->real_escape_string($_POST['production_batch']);
    $machine_number = $conn->real_escape_string($_POST['machine_number']);
    $compatible_model = $conn->real_escape_string($_POST['compatible_model']);
    $production_status = $conn->real_escape_string($_POST['production_status']);
    $remarks = $conn->real_escape_string($_POST['remarks']);

    $sql = "INSERT INTO production_details (
                model_number, 
                production_batch, 
                machine_number, 
                compatible_model, 
                production_status,
                notes,
                created_at,
                is_number_changed
            ) VALUES (
                '$model_number',
                '$production_batch',
                '$machine_number',
                '$compatible_model',
                '$production_status',
                '$remarks',
                NOW(),
                '否'
            )";

    if ($conn->query($sql)) {
        echo json_encode(['success' => true]);
    } else {
        echo json_encode(['success' => false, 'message' => $conn->error]);
    }
    exit;
}

// 获取最新的10条记录
$sql = "SELECT * FROM production_details ORDER BY created_at DESC LIMIT 10";
$recentDetails = $conn->query($sql);
?>

<!DOCTYPE html>
<html>
<head>
    <title>添加生产明细</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f4f4f4;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        .form-section {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 12px rgba(0,0,0,0.08);
            margin-bottom: 20px;
        }

        .form-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #eef2f7;
        }

        .form-header h2 {
            margin: 0;
            color: #2c3e50;
        }

        .form-grid {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 20px;
            margin-bottom: 20px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            color: #4a5568;
            font-weight: 500;
        }

        .form-control {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid #e2e8f0;
            border-radius: 6px;
            font-size: 14px;
            transition: all 0.2s;
        }

        .form-control:focus {
            outline: none;
            border-color: #4299e1;
            box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.15);
        }

        .button-group {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
            margin-top: 20px;
        }

        .btn {
            padding: 8px 16px;
            border: none;
            border-radius: 6px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.2s;
        }

        .btn-primary {
            background-color: #4299e1;
            color: white;
        }

        .btn-primary:hover {
            background-color: #3182ce;
        }

        .btn-secondary {
            background-color: #e2e8f0;
            color: #4a5568;
        }

        .btn-secondary:hover {
            background-color: #cbd5e0;
        }

        .recent-records {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 12px rgba(0,0,0,0.08);
        }

        .recent-records h3 {
            margin-top: 0;
            color: #2c3e50;
            padding-bottom: 10px;
            border-bottom: 2px solid #eef2f7;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }

        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #eef2f7;
        }

        th {
            background-color: #f8fafc;
            font-weight: 600;
            color: #3a4a5b;
        }

        tr:hover {
            background-color: #f8fafc;
        }

        .success-message {
            background-color: #9ae6b4;
            color: #276749;
            padding: 10px;
            border-radius: 6px;
            margin-bottom: 20px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="form-section">
            <div class="form-header">
                <h2>添加生产明细</h2>
                <button class="btn btn-secondary" onclick="location.href='index.php'">返回列表</button>
            </div>
            
            <div class="success-message" id="successMessage">添加成功！</div>

            <form id="addDetailForm" onsubmit="return submitForm(event)">
                <div class="form-grid">
                    <div class="form-group">
                        <label for="model_number">型号</label>
                        <input type="text" id="model_number" name="model_number" class="form-control" required>
                    </div>

                    <div class="form-group">
                        <label for="production_batch">生产批次</label>
                        <input type="text" id="production_batch" name="production_batch" class="form-control" required>
                    </div>

                    <div class="form-group">
                        <label for="machine_number">整机编号</label>
                        <input type="text" id="machine_number" name="machine_number" class="form-control" required>
                    </div>

                    <div class="form-group">
                        <label for="compatible_model">适配型号</label>
                        <input type="text" id="compatible_model" name="compatible_model" class="form-control" required>
                    </div>

                    <div class="form-group">
                        <label for="production_status">生产进度</label>
                        <select id="production_status" name="production_status" class="form-control" required>
                            <option value="未开始">未开始</option>
                            <option value="预处理上线">预处理上线</option>
                            <option value="预处理入库">预处理入库</option>
                            <option value="机芯下线">机芯下线</option>
                            <option value="机芯转涂">机芯转涂</option>
                            <option value="机芯入库">机芯入库</option>
                            <option value="壳体安装">壳体安装</option>
                            <option value="整锤转涂">整锤转涂</option>
                            <option value="整锤入库">整锤入库</option>
                            <option value="发车">发车</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="remarks">备注</label>
                        <input type="text" id="remarks" name="remarks" class="form-control">
                    </div>
                </div>

                <div class="button-group">
                    <button type="reset" class="btn btn-secondary">重置</button>
                    <button type="submit" class="btn btn-primary">添加</button>
                </div>
            </form>
        </div>

        <div class="recent-records">
            <h3>最近添加记录</h3>
            <table id="recentRecords">
                <thead>
                    <tr>
                        <th>型号</th>
                        <th>生产批次</th>
                        <th>整机编号</th>
                        <th>适配型号</th>
                        <th>生产进度</th>
                        <th>备注</th>
                        <th>添加时间</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while($detail = $recentDetails->fetch_assoc()): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($detail['model_number']); ?></td>
                            <td><?php echo htmlspecialchars($detail['production_batch']); ?></td>
                            <td><?php echo htmlspecialchars($detail['machine_number']); ?></td>
                            <td><?php echo htmlspecialchars($detail['compatible_model']); ?></td>
                            <td><?php echo htmlspecialchars($detail['production_status']); ?></td>
                            <td><?php echo htmlspecialchars($detail['notes']); ?></td>
                            <td><?php echo date('Y-m-d H:i', strtotime($detail['created_at'])); ?></td>
                        </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        </div>
    </div>

    <script>
        function submitForm(event) {
            event.preventDefault();
            
            const formData = new FormData(event.target);
            const data = {};
            formData.forEach((value, key) => data[key] = value);

            fetch('add_detail.php', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: new URLSearchParams(data)
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    // 显示成功消息
                    const successMessage = document.getElementById('successMessage');
                    successMessage.style.display = 'block';
                    
                    // 重置表单
                    document.getElementById('addDetailForm').reset();
                    
                    // 3秒后隐藏成功消息
                    setTimeout(() => {
                        successMessage.style.display = 'none';
                    }, 3000);

                    // 刷新最近记录
                    location.reload();
                } else {
                    alert('添加失败：' + (data.message || '未知错误'));
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('添加失败，请重试');
            });

            return false;
        }
    </script>
</body>
</html> 